﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    //Todo: extend this for all color spaces
    /// <summary>
    /// Color difference.
    /// This calculates a color difference.
    /// This is done by evaluating each pixel (linewise).
    /// By calculating the difference between these two pixels,
    /// the new color is set.
    /// </summary>
    public class ColorDifference : BaseImageFilter
    {
        protected int m_nScale = 3;

        protected AbstractColorSpace m_AbstractColorSpace = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDifference"/> class.
        /// </summary>
        public ColorDifference()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDifference"/> class.
        /// </summary>
        /// <param name="_nScale">The _n scale.</param>
        public ColorDifference(int _nScale)
        {
            m_nScale = _nScale;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorDifference"/> class.
        /// </summary>
        /// <param name="_colorDifference">The _color difference.</param>
        internal ColorDifference(ColorDifference _colorDifference)
        {
            this.m_nScale = _colorDifference.m_nScale;
        }

        /// <summary>
        /// Gets the scale of this filter or sets it
        /// </summary>
        /// <value></value>
        public new int Scale
        {
            get { return m_nScale; }
            set { m_nScale = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            List<Color> aColor = new List<Color>();
            aColor.Add(bitmap.GetPixel(0, 0));
            int nCount = 1;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color colorPrev = Color.Transparent;
            Color colorCurrent = Color.Transparent;
            Color colorNew = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    aColor.Add(bitmap.GetPixel(x, y));
                    colorPrev = aColor[nCount - 1];
                    colorCurrent = aColor[nCount];

                    colorNew = Color.FromArgb(
                        Math.Min(Math.Abs(colorPrev.R - colorCurrent.R) * m_nScale, 255),
                        Math.Min(Math.Abs(colorPrev.G - colorCurrent.G) * m_nScale, 255),
                        Math.Min(Math.Abs(colorPrev.B - colorCurrent.B) * m_nScale, 255)
                        );
                    bitmap.SetPixel(x, y, colorNew);
                    nCount++;
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Gets or sets the abstract color space.
        /// </summary>
        /// <value>The abstract color space.</value>
        public AbstractColorSpace AbstractColorSpace
        {
            get { return m_AbstractColorSpace; }
            set { m_AbstractColorSpace = value; }
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ColorDifference(this);
        }
    }
}
